﻿Imports WiremetModel.Entities.SupplyOrders
Imports WiremetModel.Entities.OEM
Imports InputValidation
Imports InputValidation.GeneralRules
Imports InputValidation.GeneralRules.NumericInputRules
Imports System.ComponentModel


Public Class DieOrderLineInputDialog


    Private _dieOrderLine As DieOrderLine
    Public Property DieOrderLine() As DieOrderLine
        Get
            Return _dieOrderLine
        End Get
        Set(ByVal value As DieOrderLine)
            _dieOrderLine = value
        End Set
    End Property

    Private _inputValidator As InputValidator

    Public Sub New()
        InitializeComponent()
        _dieOrderLine = New DieOrderLine()
        _inputValidator = New InputValidator()
        ClearErrorLabels()
    End Sub


    Public Sub New(ByVal dieOrderLine As DieOrderLine)
        Me.New()

        tb_Diameter.Text = dieOrderLine.Diameter.ToString()
        tb_CasingDiameter.Text = dieOrderLine.CasingDiameter.ToString()
        tb_CasingHeight.Text = dieOrderLine.CasingHeight.ToString()
        tb_Quantity.Text = dieOrderLine.Quantity.ToString()
        tb_DiameterToleranceMinus.Text = dieOrderLine.DiameterToleranceMinus.ToString()
        tb_DiameterTolerancePlus.Text = dieOrderLine.DiameterTolerancePlus.ToString()

        _dieOrderLine = dieOrderLine
    End Sub

    Private Sub ClearErrorLabels()
        lbl_CasingDiameterError.Text = String.Empty
        lbl_CasingHeightError.Text = String.Empty
        lbl_DiameterError.Text = String.Empty
        lbl_DiameterToleranceMinusError.Text = String.Empty
        lbl_DiameterTolerancePlusError.Text = String.Empty
        lbl_QuantityError.Text = String.Empty
    End Sub


#Region "Input validation"

    Private Sub tb_Diameter_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles tb_Diameter.Validating
        Dim notNullableTextInputValidatingSpecification As New NotNullableTextInputValidatingSpecification()
        Dim numericInputValidatingSpecification As New NumericInputValidatingSpecification()
        Dim numericPositiveInputValidatingSpecification As New NumericPositiveInputValidatingSpecification()
        _inputValidator.ValidateTextBox(tb_Diameter, e, lbl_DiameterError, notNullableTextInputValidatingSpecification.And(numericInputValidatingSpecification).And(numericPositiveInputValidatingSpecification))
    End Sub

    Private Sub tb_DiameterToleranceMinus_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles tb_DiameterToleranceMinus.Validating
        Dim notNullableTextInputValidatingSpecification As New NotNullableTextInputValidatingSpecification()
        Dim numericInputValidatingSpecification As New NumericInputValidatingSpecification()
        _inputValidator.ValidateTextBox(tb_DiameterToleranceMinus, e, lbl_DiameterToleranceMinusError, notNullableTextInputValidatingSpecification.And(numericInputValidatingSpecification))
    End Sub

    Private Sub tb_DiameterTolerancePlus_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles tb_DiameterTolerancePlus.Validating
        Dim notNullableTextInputValidatingSpecification As New NotNullableTextInputValidatingSpecification()
        Dim numericInputValidatingSpecification As New NumericInputValidatingSpecification()
        _inputValidator.ValidateTextBox(tb_DiameterToleranceMinus, e, lbl_DiameterToleranceMinusError, notNullableTextInputValidatingSpecification.And(numericInputValidatingSpecification))

        If Not e.Cancel Then
            Dim diameterTolerancePlusValidatingSpecification As New DiameterTolerancePlusValidatingSpecification(Decimal.Parse(tb_DiameterToleranceMinus.Text.Trim()))
            _inputValidator.ValidateTextBox(tb_DiameterTolerancePlus, e, lbl_DiameterTolerancePlusError, notNullableTextInputValidatingSpecification.And(numericInputValidatingSpecification).And(diameterTolerancePlusValidatingSpecification))
        End If
    End Sub

    Private Sub tb_CasingDiameter_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles tb_CasingDiameter.Validating
        Dim notNullableTextInputValidatingSpecification As New NotNullableTextInputValidatingSpecification()
        Dim numericInputValidatingSpecification As New NumericInputValidatingSpecification()
        Dim numericPositiveInputValidatingSpecification As New NumericPositiveInputValidatingSpecification()
        _inputValidator.ValidateTextBox(tb_CasingDiameter, e, lbl_CasingDiameterError, notNullableTextInputValidatingSpecification.And(numericInputValidatingSpecification).And(numericPositiveInputValidatingSpecification))
    End Sub

    Private Sub tb_CasingHeight_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles tb_CasingHeight.Validating
        Dim notNullableTextInputValidatingSpecification As New NotNullableTextInputValidatingSpecification()
        Dim numericInputValidatingSpecification As New NumericInputValidatingSpecification()
        Dim numericPositiveInputValidatingSpecification As New NumericPositiveInputValidatingSpecification()
        _inputValidator.ValidateTextBox(tb_CasingHeight, e, lbl_CasingHeightError, notNullableTextInputValidatingSpecification.And(numericInputValidatingSpecification).And(numericPositiveInputValidatingSpecification))
    End Sub

    Private Sub tb_Quantity_Validating(ByVal sender As Object, ByVal e As CancelEventArgs) Handles tb_Quantity.Validating
        Dim notNullableTextInputValidatingSpecification As New NotNullableTextInputValidatingSpecification()
        Dim integerInputValidatingSpecification As New IntegerInputValidatingSpecification()
        Dim numericPositiveInputValidatingSpecification As New NumericPositiveInputValidatingSpecification()
        _inputValidator.ValidateTextBox(tb_Quantity, e, lbl_QuantityError, notNullableTextInputValidatingSpecification.And(integerInputValidatingSpecification).And(numericPositiveInputValidatingSpecification))
    End Sub


    Private Sub tb_Diameter_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles tb_Diameter.Validated
        lbl_DiameterError.Text = String.Empty
    End Sub

    Private Sub tb_DiameterToleranceMinus_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles tb_DiameterToleranceMinus.Validated
        lbl_DiameterToleranceMinusError.Text = String.Empty
    End Sub

    Private Sub tb_DiameterTolerancePlus_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles tb_DiameterTolerancePlus.Validated
        lbl_DiameterTolerancePlusError.Text = String.Empty
    End Sub

    Private Sub tb_CasingDiameter_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles tb_CasingDiameter.Validated
        lbl_CasingDiameterError.Text = String.Empty
    End Sub

    Private Sub tb_CasingHeight_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles tb_CasingHeight.Validated
        lbl_CasingHeightError.Text = String.Empty
    End Sub

    Private Sub tb_Quantity_Validated(ByVal sender As Object, ByVal e As EventArgs) Handles tb_Quantity.Validated
        lbl_QuantityError.Text = String.Empty
    End Sub

#End Region

    Private Sub btn_Accept_Click(sender As System.Object, e As System.EventArgs) Handles btn_Accept.Click
        Try
            If Not ValidateChildren() Then
                Throw New EntityDataInputException("Podane dane są nieprawidłowe." & vbNewLine &
                                                   "Popraw je i spróbuj ponownie.")
            End If

            Dim confirmationDialogResult As DialogResult = MessageBox.Show("Czy jesteś pewień?", "Uwaga!", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
            If confirmationDialogResult = Windows.Forms.DialogResult.Yes Then

                With DieOrderLine
                    .CasingDiameter = Decimal.Parse(tb_CasingDiameter.Text.Trim())
                    .CasingHeight = Decimal.Parse(tb_CasingHeight.Text.Trim())
                    .Diameter = Decimal.Parse(tb_Diameter.Text.Trim())
                    .DiameterToleranceMinus = Decimal.Parse(tb_DiameterToleranceMinus.Text.Trim())
                    .DiameterTolerancePlus = Decimal.Parse(tb_DiameterTolerancePlus.Text.Trim())
                    .Quantity = Integer.Parse(tb_Quantity.Text.Trim())
                End With

                DialogResult = Windows.Forms.DialogResult.OK
            End If
        Catch ex As EntityDataInputException
            MessageBox.Show(ex.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning)
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub btn_Cancel_Click(sender As System.Object, e As System.EventArgs) Handles btn_Cancel.Click
        DialogResult = Windows.Forms.DialogResult.Cancel
    End Sub
End Class